package xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.command;

import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.Editor;
import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.History;
import xing.design.pattern.behavioral.Memento.sample3_andCommandPattern.editor.Pair;

/**
 * @author xing
 * @date 2023/2/10 15:57:49
 */
public class UndoCommand implements ICommand {

    /**
     * 对于 Undo 和 Redo ，不保存到 History ，所以这里重写 execute 方法
     */
    @Override
    public void execute(Editor editor) {
        History history = editor.getHistory();
        Pair pair = history.undoPop();
        if (pair == null) {
            return;
        }
        history.redoPush(pair.getC());
        Pair pair1 = history.undoPeek();
        if (pair1 == null) {
            editor.clear();
            return;
        }
        pair1.getM().restore();
    }


    @Override
    public String getName() {
        return "undo";
    }

}
